﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Web.Routing;

namespace SF.WebClient.Helper
{
    public static class FileHelper
    {
        /// <summary>
        /// 以 utf-8 编码方式读取文件内容
        /// </summary>
        /// <param name="path">文件相对路径（含文件全名）</param>
        /// <returns>文件内容</returns>
        public static string ReadFile(string path)
        {
            return ReadFile(path, System.Text.Encoding.GetEncoding("utf-8"));
        }

        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="path">文件相对路径（含文件全名）</param>
        /// <param name="encoding">字符编码方式</param>
        /// <returns>文件内容</returns>
        public static string ReadFile(string path, System.Text.Encoding encoding)
        {
            try
            {
                using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(path), encoding))
                {
                    return sr.ReadToEnd().ToString();
                }
            }
            catch
            {
                return "<span style='color:red; font-size:x-large;'>文件不存在！</span>";
            }
        }
    }

}